package com.elitel.common.utils;


import com.alibaba.fastjson.JSONObject;
import com.elitel.common.core.constant.SysUserConstant;
import com.elitel.common.core.domain.SysUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpSession;

/**
 * @author mulis
 */
public class SysUserUtils {
    private static final Logger logger = LoggerFactory.getLogger(SysUserUtils.class);


    public static String getLoginAccount() {
        try {
            HttpSession session = ServletUtils.getSession();
            return (String) session.getAttribute(SysUserConstant.LOGIN_ACCOUNT);
        } catch (Exception e) {
            logger.error("获取用户登录账号失败", e);
            return null;
        }
    }


    public static SysUserInfo getUserInfo() {
        try {
            String json = (String) ServletUtils.getSession().getAttribute(SysUserConstant.LOGIN_USER_INFO);
            return JSONObject.parseObject(json, SysUserInfo.class);
        } catch (Exception e) {
            logger.warn("获取用户信息失败，请检查当前接口调用是否登录系统!!! 异常信息为:{}", e.getMessage());
            return null;
        }

    }

    public static String getRoleIds() {
        try {
            SysUserInfo userInfo = getUserInfo();
            return userInfo == null ? null : userInfo.getRoleIds();
        } catch (Exception e) {
            logger.error("获取用户角色id失败，{}", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}
